<?php
/*
 *
 * @package MoltX
 * @author $Author$
 * @version $Id$
 * @link $URL$
 * @copyright Copyright (c) 2005-2010, Matthew Oltendorf
 * @license http://opensource.org/licenses/gpl-license.php GNU Public License
 *
 */

/*
 * @ignore
 */
if ( ! defined ( 'blink' ) )
{
	// Access denied.
	header ( $_SERVER [ 'SERVER_PROTOCOL' ] . ' 403 Forbidden' );
	echo '403 - Forbidden';

	exit;
}

/*
 * phpBB module class
 * @package Blink
 *
 * Singleton
 */
final class blink_module
{
	private static $room = null;

	/*
	 * Constructor function
	 *
	 * This is private to enforce singleton mode.
	 *
	 * @return null
	 */
	private function __construct ( )
	{
		return null;
	}

	/*
	 * Initiate function
	 *
	 * Start the module.
	 *
	 * @return null
	 */
	public static function initiate ( )
	{
		// Development only. @todo: Remove for production.
		blink_console :: log ( 'room spawned' );

		/*
		 * Let's find out, are we single execution, permanent single-threaded,
		 * or permanent multi-threaded?
		 */

		// Permanent.
		if ( blink :: $server )
		{
			// Load our server module.
			include blink_system . '/server/' . blink :: $server . '.php';

			if ( blink :: $room )
			{
				// We should only ever receive an integer from this variable.
				blink :: $room = (integer) blink :: $room;

				self :: $room = new blink_room ( blink :: $room );
			}

			// @todo: Change to a higher interval for production.
			blink_server :: listen ( 'handler', false, 1, array ( __CLASS__, 'response' ) );
		}
		// Single execution.
		else
		{

		}

		// Development only. @todo: Remove for production.
		blink_console :: log ( 'room closed' );

		return null;
	}

	/*
	 * Response function
	 *
	 * @return null
	 */
	public static function response ( )
	{
		$close = false;

		if ( isset ( $data [ 'actions' ] ) )
		{
			try
			{
				if ( is_array ( $data [ 'actions' ] ) )
				{
					$actions = &$data [ 'actions' ];
				}
				else
				{
					$actions = array ( &$data [ 'actions' ] );
				}

				for ( $i = 0, $total = count ( $actions ); $i < $total; ++$i )
				{
					switch ( $actions [ $i ] )
					{
						case 'close':
							$close = true;
						break;

						default:
							throw new Exception ( 'INVALID ACTION' );
						break;
					}
				}
			}
			catch ( Exception $exception )
			{
				// Bad input, ignore.

				// Development only. @todo: Remove for production.
				blink_console :: log ( 'Invalid Data: ' . $input );
			}
		}

		return ! $close;
	}
}
